package com.rdgk.newtech.controller;

import com.alibaba.fastjson.JSONObject;
import com.rdgk.newtech.service.DataDicService;
import com.rdgk.newtech.util.CommonUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: msun
 * @description: 数据字典相关Controller
 */
@RestController
@RequestMapping("/dataDic")
public class DataDicController {

    @Autowired
    private DataDicService dataDicService;

    /**
     * 显示数据字典类型列表
     */
    @RequiresPermissions("article:list")
    @GetMapping("/listDicType")
    public JSONObject listDataDicType(HttpServletRequest request) {
        System.out.println(CommonUtil.request2Json(request));
        return dataDicService.listDataDicType();
    }

    /**
     * 新增数据字典类型列表
     */
    @RequiresPermissions("article:add")
    @PostMapping("/addDicType")
    public JSONObject addDicType(@RequestBody JSONObject requestJson) {
        System.out.println("requestJson:"+requestJson);
        CommonUtil.hasAllRequired(requestJson, "typeMark, typeName, typeCode, typeId");
        return dataDicService.addDataDicType(requestJson);
    }

    /**
     * 修改数据字典类型列表
     */
    @RequiresPermissions("article:update")
    @PostMapping("/updateDicType")
    public JSONObject updateDicType(@RequestBody JSONObject requestJson) {
        System.out.println("requestJson:"+requestJson);
        CommonUtil.hasAllRequired(requestJson, "typePid, typeMark, typeName, typeCode");
        return dataDicService.updateDataDicType(requestJson);
    }

    /**
     * 删除数据字典列表
     */
    @RequiresPermissions("article:delete")
    @PostMapping("/deleteDicType")
    public JSONObject deleteDicType(@RequestBody JSONObject requestJson) {
        System.out.println("requestJson:"+requestJson);
        CommonUtil.hasAllRequired(requestJson, "typeId");
        return dataDicService.deleteDataDicType(requestJson);
    }

    /**
     * 显示数据字典列表
     */
    @RequiresPermissions("article:list")
    @GetMapping("/listDicData")
    public JSONObject listDataDic(HttpServletRequest request) {
        System.out.println(CommonUtil.request2Json(request));
        return dataDicService.listDataDic(CommonUtil.request2Json(request));
    }

    /**
     * 显示搜索后的数据字典列表
     */
    @RequiresPermissions("article:list")
    @GetMapping("/searchDicData")
    public JSONObject listDataDicBySearch(HttpServletRequest request) {
        System.out.println(CommonUtil.request2Json(request));
        return dataDicService.listDataDicBySearch(CommonUtil.request2Json(request));
    }

    /**
     * 新增数据字典类型列表
     */
    @RequiresPermissions("article:add")
    @PostMapping("/addDic")
    public JSONObject addDic(@RequestBody JSONObject requestJson) {
        System.out.println("requestJson:"+requestJson);
        CommonUtil.hasAllRequired(requestJson, "dataTypeId, dataCode, dataName, typeState, dataMark");
        return dataDicService.addDataDic(requestJson);
    }

    /**
     * 修改数据字典类型列表
     */
    @RequiresPermissions("article:update")
    @PostMapping("/updateDic")
    public JSONObject updateDic(@RequestBody JSONObject requestJson) {
        System.out.println("requestJson:"+requestJson);
        CommonUtil.hasAllRequired(requestJson, "dataId, dataCode, dataName, typeState, dataMark");
        return dataDicService.updateDataDic(requestJson);
    }

    /**
     * 删除数据字典列表
     */
    @RequiresPermissions("article:delete")
    @PostMapping("/deleteDic")
    public JSONObject deleteDic(@RequestBody JSONObject requestJson) {
        System.out.println("requestJson:"+requestJson);
        CommonUtil.hasAllRequired(requestJson, "id");
        return dataDicService.deleteDataDic(requestJson);
    }
}
